package com.frxuexi.park2305.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.mymvi.R
import com.frxuexi.mymvi.databinding.ActivityNewsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NewsActivity")
@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsIntent, NewsViewModel>() {
    override val binding: ActivityNewsBinding by lazy { ActivityNewsBinding.inflate(layoutInflater) }
    override val vm: NewsViewModel by viewModels<NewsViewModel>()
    val newsAdapter by lazy { NewsAdapter() }
    var position = -1
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DELETE -> {
                                newsAdapter.data.removeAt(position)
                                newsAdapter.notifyItemRemoved(position)
                            }

                            else -> {
                                var items = it.data as List<NewsBeanItem>
                                items = items.filter { it.published }.sortedByDescending { it.id }
                                for (i in 0..items.size - 1) {
                                    items.get(i).mytype = i % 2
                                }
                                newsAdapter.apply {
                                    data.clear()
                                    addData(items)
                                    notifyDataSetChanged()
                                }
                            }
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.newsRv.apply {
            adapter = newsAdapter
            layoutManager = LinearLayoutManager(this@NewsActivity)
        }

        newsAdapter.addChildClickViewIds(
            com.frxuexi.park2305.R.id.new_detail,
            com.frxuexi.park2305.R.id.new_detail2
        )
        newsAdapter.setOnItemChildClickListener { adapter, view, position ->
            this.position = position
            vm.sendIntent(NewsIntent.revokeNews(newsAdapter.data.get(position).id!!))
        }

        newsAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/app/NewsAddActivity",newsAdapter.data.get(position).id)
        }

        binding.newsAdd.setOnClickListener {
            ArouterManager.nav(this,"/app/NewsAddActivity")
        }

        binding.newsSv.setOnQueryTextListener(object : OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.sendIntent(NewsIntent.getNewsByKey(query!!))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    vm.sendIntent(NewsIntent.getNews)
                }
                return true
            }
        })

        ItemTouchHelper(object :
            ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT, ItemTouchHelper.LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                position = viewHolder.adapterPosition
                vm.sendIntent(NewsIntent.deleteNews(newsAdapter.data.get(position).id!!))
            }
        }).attachToRecyclerView(binding.newsRv)
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(NewsIntent.getNews)
    }

}